<template>
  <div class="container clearfix">
    <div class="filter">
      <h2 class="title">
        筛选查询
      </h2>
      <div class="section">
        <span class="tip">
          会员姓名
        </span>
        <div class="content">
          <el-input
            v-model="search_form.realname"
            placeholder="请输入姓名"
          />
        </div>
      </div>
      <div class="section">
        <span class="tip">
          会员手机号
        </span>
        <div class="content">
          <el-input
            v-model="search_form.username"
            placeholder="请输入手机号"
          />
        </div>
      </div>
      <div class="section">
        <span class="tip">
          状态
        </span>
        <el-select
          v-model="search_form.status"
          placeholder="请选择"
        >
          <el-option
            v-for="item in status_arr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="btn-bar1">
        <el-button
          type="primary"
          siae="small"
          icon="el-icon-search"
          @click="search()"
        >查询结果</el-button>
      </div>
      <div class="btn-bar2">
        <el-button
          type="success"
          siae="small"
          icon="el-icon-search"
          @click="reset()"
        >重置</el-button>
      </div>
    </div>
    <div class="data-table">
      <el-table
        v-loading="table_loading"
        :data="tableData"
        border
        height="660"
        align="center"
        style="width: 100%"
      >
        <el-table-column
          prop="userId"
          align="center"
          label="会员id"
        />
        <el-table-column
          prop="realname"
          align="center"
          label="会员姓名"
        />
        <el-table-column
          prop="username"
          align="center"
          label="会员电话"
        />
        <el-table-column
          prop="balance"
          align="center"
          label="账户余额"
        />
        <el-table-column
          prop="applyTime"
          align="center"
          label="申请时间"
        />
        <el-table-column
          prop="withdrawalAmount"
          align="center"
          label="提现金额"
        />
        <el-table-column
          prop="bankName"
          align="center"
          label="银行卡名"
        />
        <el-table-column
          prop="bankCardNum"
          align="center"
          label="银行卡号"
        />
        <el-table-column
          prop="status"
          align="center"
          label="状态"
        />
        <el-table-column
          fixed="right"
          label="操作"
          align="center"
          width="320"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status === '待审核'"
              size="media"
              @click="pass_apply(scope.row)"
            >
              审核通过
            </el-button>
            <el-button
              type="success"
              size="media"
              @click="user_info(scope.row)"
            >
              查看会员信息
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 20px">
      <el-button @click="exports()">导出</el-button>
    </div>
    <!-- 分页器 -->
    <div class="block">
      <el-pagination
        background
        :page-sizes="[10,20,50,100]"
        :page-size="pageSize"
        :pager-count="11"
        layout="total, sizes, prev, pager, next"
        :total="total"
        @current-change="change_page"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>
<script>
import { getHospitalList } from '@/api/hospitalManage'
import { downloadWithdrawFile, exportWithdrawFile } from '@/api/file'
import { passWithdraw, withdrawList } from '@/api/cash'
export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      value1: '',
      value2: '',
      value: true,
      table_loading: false,
      class_arr: [
      ],
      status_arr: [
        {
          value: null,
          label: '全部'
        },
        {
          value: 1,
          label: '审核中'
        }, {
          value: 2,
          label: '审核通过'
        }
      ],
      total: 0, // 分页总数
      tableData: [],
      hospitalDate: '',
      page: 1,
      pageSize: 10,
      search_form: {
        //   姓名
        realname: '',
        //   电话
        username: '',
        // 审核状态
        status: null
      },
      multipleSelection: [],
      model: 0// 0 初次加载   1表示条件筛选
    }
  },
  //新加路由保存参数
  beforeRouteLeave(to, from, next) {
    // const answer = window.confirm("当前页面搜索条件将会被保存");
    //更新 put
    let keyname = this.$route.name;
    let value = JSON.stringify(this.search_form);
    sessionStorage.setItem(keyname, value);
    next();
  },
  mounted() {
    if (sessionStorage.getItem(this.$route.name)) {
      this.search_form = JSON.parse(sessionStorage.getItem(this.$route.name));
    }
    this.search()
    // 获取医院列表
    this.get_hospital_list()
  },
  methods: {
    // 重置
    reset() {
      this.table_loading = true
      this.$router.go(0)
      this.table_loading = false
    },
    // 筛选
    search() {
      this.model = 1
      this.page = 1
      this.get_list({
        'pageNum': this.page,
        'pageSize': this.pageSize,
        'status': this.search_form.status,
        'username': this.search_form.username,
        'realname': this.search_form.realname
      })
    },

    // 获取列表
    get_list(options = {
      'pageNum': this.page,
      'pageSize': this.pageSize,
      'status': this.search_form.status,
      'username': this.search_form.username,
      'realname': this.search_form.realname
    }) {
      this.table_loading = true
      withdrawList(options).then((result) => {
        const res = result.data
        if (res.code === 0) {
          this.tableData = res.data.list
          console.log(this.tableData)
          this.total = res.data.count
          this.table_loading = false
        }
      }).catch((err) => {
        this.table_loading = false
        this.$message.error(err)
      })
    },

    // 查看用户详情
    user_info(row) {
      console.log(row)
      this.$router.push({
        path: '/user_detail',
        query: {
          user_id: row.userId
        }
      })
    },
    // 导出
    exports() {
      var option = {
        'pageNum': this.page,
        'pageSize': this.pageSize,
        'status': this.search_form.status,
        'username': this.search_form.username,
        'realname': this.search_form.realname
      }
      // 调用生成导出文件接口
      exportWithdrawFile(option).then((result) => {
        // 调用下载导出文件接口
        if (result.data.code === 0) {
          downloadWithdrawFile( {
            'fileName': result.data.msg
          }, { responseType: 'arraybuffer' }).then((res) => {
            const blob = new Blob([res.data], { type: 'application/x-xlsx' })
            if (window.navigator.msSaveOrOpenBlob) {
              navigator.msSaveBlob(blob, fileName)
            } else {
              var link = document.createElement('a')
              link.href = window.URL.createObjectURL(blob)
              link.download = result.data.msg
              link.click()
              window.URL.revokeObjectURL(link.href)
            }
            if (res.data.code === 0) {
              this.$message.success(res.data.msg)
            } else {
              this.$message.error(res.data.msg)
            }
          })
        } else {
          this.$message.error(result.data.msg)
        }
      })
    },
    // 体现审核通过
    pass_apply(row) {
      this.$confirm('确定审核通过？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        passWithdraw({
          'id': row.id
        }).then((result) => {
          const res = result.data
          if (res.code === 0) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.search()
          } else {
            this.$message.error(res.msg)
          }
        }).catch((err) => {
          this.$message.error('服务器网络错误，请稍后重试')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    // 获取医院列表
    get_hospital_list(){
      getHospitalList( {
        pageNum: this.page,
        pageSize: this.pageSize
      }).then(res => {
        this.hospitalDate = res.data.data.list
      }).catch(res => {
        this.$message.error('网络连接错误，请稍后再试')
      })
    },

    // 分页
    change_page(page) {
      this.page = page
      this.search()
    },
    handleSizeChange(page) {
      this.pageSize = page
      this.search()
    }
  }
}
</script>
<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  .filter {
    margin: 20px;
    background-color: #fff;
    box-shadow: 1px 1px 30px rgba(0, 0, 0, 0.1);
    border-radius: 12px;
    padding: 20px;
    position: relative;
    .title {
      font-size: 16px;
    }
    .section {
      display: inline-block;
      vertical-align: middle;
      margin-right: 20px;
      margin-top: 16px;
      .tip {
        font-size: 14px;
        display: inline-block;
        vertical-align: middle;
        margin-right: 5px;
      }
      .content {
        display: inline-block;
        vertical-align: middle;
      }
      .inp {
        display: inline-block;
        vertical-align: middle;
        border: none;
        outline: none;
        border: 1px solid #ccc;
        border-radius: 4px;
        padding: 6px;
      }
    }
    .btn-bar1 {
      position: absolute;
      right: 200px;
      top: 50px;
    }
    .btn-bar2 {
      position: absolute;
      right: 80px;
      top: 50px;
    }
  }
  .data-table {
    margin: 20px;
    background-color: #fff;
    box-sizing: border-box;
  }
  .page-controller {
    float: right;
    margin-right: 20px;
  }
}
</style>

